package pers.hubb.dao.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import pers.hubb.dao.ITUserDODao;
import pers.hubb.domain.TUserDO;

import java.util.List;

@Repository
public class ITUserDODaoImpl implements ITUserDODao {

    @Autowired
    private JdbcTemplate jdbcTemplate;

    /**
     * 新增
     *
     * @param userDO
     * @return
     */
    @Override
    public int add(TUserDO userDO) {
        return jdbcTemplate.update("insert into t_user values(?,?)", userDO.getRealName(), userDO.getNickName());
    }

    /**
     * 修改
     *
     * @param userDO
     * @return
     */
    @Override
    public int update(TUserDO userDO) {
        return jdbcTemplate.update("update t_user set real_name = ?, nick_name = ? where id = ?",
                new Object[]{userDO.getRealName(), userDO.getNickName(), userDO.getId()});
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @Override
    public int delete(Long id) {
        return jdbcTemplate.update("delete from t_user where id = ?", id);
    }

    /**
     * 查询
     *
     * @param id
     * @return
     */
    @Override
    public TUserDO findTUserDO(Long id) {
        List<TUserDO> list = jdbcTemplate.query("select * from t_user where id = ?", new Object[]{id}, new
                BeanPropertyRowMapper(TUserDO.class));
        if (null != list && list.size() > 0) {
            TUserDO userDO = list.get(0);
            return userDO;
        } else {
            return null;
        }
    }

    /**
     * 获取全部数据
     *
     * @return
     */
    @Override
    public List<TUserDO> findTUserDOList() {
        return jdbcTemplate.query("select * from t_user", new Object[]{}, new BeanPropertyRowMapper<TUserDO>(TUserDO
                .class));
    }
}
